﻿using OdeToFood.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OdeToFood.Controllers
{
    public class ReviewsController : Controller
    {
        //
        // GET: /Reviews/

        OdeToFoodDb _db = new OdeToFoodDb();
        public ActionResult Index([Bind(Prefix="id")] int restaurantId )
        {
            var restaurant = _db.Restaurants.Find(restaurantId);
            if (restaurant !=null)
            {
                return View(restaurant);
            }
            return HttpNotFound();
 
        }

        [HttpGet]
        public ActionResult Create(int restaurantId)
        {

            return View();

        }
        [HttpPost]
        public ActionResult Create(RestaurantReview review)
        {
            if (ModelState.IsValid)
            {
                
            _db.Reviews.Add(review);
            _db.SaveChanges();
            return RedirectToAction("Index", new {id=review.RestaurantId});
            }
            return View(review);
        }

        [HttpPost]

        public ActionResult Edit(RestaurantReview review)
        {
            if (ModelState.IsValid)
            {

                _db.Entry(review).State= EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = review.RestaurantId });
            }
            return View(review);
        }


        [HttpGet]
        public ActionResult Edit(int id)
        {
            var model = _db.Reviews.Find(id);
            return View(model);


        }




        protected override void Dispose(bool disposing)
        {
            _db.Dispose();

            base.Dispose(disposing);
        }
    }
   

}
